import { getProjectId, removeProjectId, setProjectId,getProjectRole, setProjectRole, removeProjectRole } from '@/utils/auth'

const state = {
  id:getProjectId(),
  role:getProjectRole()
}

const mutations = {
  SET_PROJECT:(state,pro_id) => {
    state.id = pro_id
  },
  REMOVE_PROJECT:(state) => {
    state.id = getProjectId()
  },
  SET_PROJECTROLE:(state, pro_role) =>{
    state.role = pro_role
  },
  REMOVE_PROJECTROLE:(state) =>{
    state.role = getProjectRole()
  }
}

const  actions = {
  setProject({commit},project){
    return new Promise((resolve) => {
      commit('SET_PROJECTROLE',project.role)
      setProjectRole(project.role)
      commit('SET_PROJECT', project.id)
      setProjectId(project.id)
      resolve()
    })
  },
  removeProject({commit}){
    return new Promise((resolve) => {
      commit('REMOVE_PROJECT')
      removeProjectId()
      commit('REMOVE_PROJECTROLE')
      removeProjectRole()
      resolve()
    })
  },
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
